<template>
	<a-modal :visible="visible" :title="title" @Ok="fun_submit" @cancel="fun_cancel">
		<a-form name="tt" v-model="formData" :label-col="{ span: 4 }" :wrapper-col="{ span: 18 }">
			<template v-for="item in columns" :key="item.dataIndex">
				<a-form-item v-if="item.iskey" :label="item.title" :name="item.dataIndex">
					<a-input v-model:value="formData[item.dataIndex]" disabled />
				</a-form-item>
				<a-form-item v-else-if="item.edit" :label="item.title" :name="item.dataIndex">
					<a-date-picker v-if="item.type == 'date'" v-model:value="formData[item.dataIndex]" placeholder="选择日期" />
					<a-date-picker v-else-if="item.type == 'datetime'" show-time v-model:value="formData[item.dataIndex]" placeholder="选择时间" />
					<a-time-picker v-else-if="item.type == 'time'" v-model:value="formData[item.dataIndex]" placeholder="选择时间" />

					<a-textarea v-else-if="item.type == 'text'" v-model:value="formData[item.dataIndex]" />
					<a-radio-group v-else-if="item.type == 'radio'" v-model:value="formData[item.dataIndex]" :options="item.dict" />
					<a-checkbox-group v-else-if="item.type == 'checkbox'" v-model:value="formData[item.dataIndex]" name="checkboxgroup" :options="item.dict" />
					<a-select v-else-if="item.type == 'select'" v-model:value="formData[item.dataIndex]" :options="item.dict"></a-select>

					<a-input-number v-else-if="(item.type == 'int' || item.type == 'long' || item.type == 'decimal')" v-model:value="formData[item.dataIndex]" style="width: 100%"/>
					<a-input v-else v-model:value="formData[item.dataIndex]" />
				</a-form-item>
			</template>
		</a-form>
	</a-modal>
</template>

<script>
export default {
	name: 'tableEdit',
	props: ['columns', 'visible', 'editUrl', 'getUrl'],
	data() {
		return {
			title: '编辑',
			formData: {},
		};
	},
	mounted() {},
	methods: {
		/**
		 * @function 加载数据
		 * @param {*} id  主键id
		 */
		fun_getData(id) {
			var that = this;
			that.$http.post(that.getUrl, { id: id }, that, function (ret) {
				that.formData = ret.Data;

				//时间格式化处理
				that.columns.forEach((elem, index) => {
					if (elem.type == 'date' || elem.type == 'datetime' || elem.type == 'time') {
						that.formData[elem.dataIndex] = that.$dayjs(that.formData[elem.dataIndex]);
					}
				});
			});
		},
		/**
		 * @function 提交修改
		 */
		fun_submit() {
			var that = this;
			debugger
			that.$http.post(that.editUrl, that.formData, that, function (ret) {
				that.$Message['success']({
					background: true,
					duration: 1,
					content: '更新成功',
				});
				that.$parent.fatherRefresh(false, true, 'edit');
			});
		},
		/**
		 * @function 取消关闭窗口
		 */
		fun_cancel() {
			this.$parent.fatherRefresh(false, false, 'edit');
		},
	},
};
</script>

<style></style>
